<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>linux 源代码分析11:Slab初始化 | 机器人的博客</title>
  <meta name="keywords" content=" linux , 内存管理 , 伙伴系统 , Slab ">
  <meta name="description" content="linux 源代码分析11:Slab初始化 | 机器人的博客">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="页面未找到！">
<meta property="og:type" content="website">
<meta property="og:title" content="404">
<meta property="og:url" content="http://ancjf.com/404.html">
<meta property="og:site_name" content="机器人的博客">
<meta property="og:description" content="页面未找到！">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-04-18T07:23:22.000Z">
<meta property="article:modified_time" content="2020-04-18T07:23:50.777Z">
<meta property="article:author" content="ancjf">
<meta name="twitter:card" content="summary">


<link rel="icon" href="/img/avatar.jpg">

<link href="/css/style.css?v=1.0.1" rel="stylesheet">

<link href="/css/hl_theme/atom-light.css?v=1.0.1" rel="stylesheet">

<link href="//cdn.bootcss.com/animate.css/3.5.2/animate.min.css" rel="stylesheet">
<link href="//cdn.bootcss.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet">

<script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
<script src="/js/jquery.autocomplete.min.js?v=1.0.1" ></script>

<script src="//cdn.bootcss.com/highlight.js/9.12.0/highlight.min.js"></script>
<script>
    hljs.initHighlightingOnLoad();
</script>

<script src="//cdn.bootcss.com/nprogress/0.2.0/nprogress.min.js"></script>



<script src="//cdn.bootcss.com/jquery-cookie/1.4.1/jquery.cookie.min.js" ></script>

<script src="/js/iconfont.js?v=1.0.1" ></script>

<meta name="generator" content="Hexo 4.2.0"></head>
<div style="display: none">
  <input class="theme_disqus_on" value="false">
  <input class="theme_preload_comment" value="false">
  <input class="theme_blog_path" value="">
</div>

<body>
<aside class="nav">
    <div class="nav-left">
        <a href="/" class="avatar_target">
    <img class="avatar" src="/img/avatar.jpg" />
</a>
<div class="author">
    <span>ancjf</span>
</div>

<div class="icon">
    
        
        <a title="github" href="https://github.com/ancjf" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-github"></use>
                </svg>
            
        </a>
        
    
        
        <a title="zhihu" href="https://www.zhihu.com/people/ancjf" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-zhihu"></use>
                </svg>
            
        </a>
        
    
        
        <a title="csdn" href="http://blog.csdn.net/ancjf" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-csdn"></use>
                </svg>
            
        </a>
        
    
        
        <a title="email" href="mailto:ancjf@163.com" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-email"></use>
                </svg>
            
        </a>
        
    
        
        <a title="qq" href="http://wpa.qq.com/msgrd?v=3&uin=156715133&site=qq&menu=yes" target="_blank">
            
                <svg class="iconfont-svg" aria-hidden="true">
                    <use xlink:href="#icon-qq"></use>
                </svg>
            
        </a>
        
    
</div>




<ul>
    <li><div class="all active">全部文章<small>(26)</small></div></li>
    
        
            
            <li><div data-rel="linux">linux<small>(12)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="区块链">区块链<small>(8)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="c++">c++<small>(4)</small></div>
                
            </li>
            
        
    
        
            
            <li><div data-rel="react native">react native<small>(2)</small></div>
                
            </li>
            
        
    
</ul>
<div class="left-bottom">
    <div class="menus">
    
    
    
    
    </div>
    <div><a class="about  hasFriend  site_url"  href="/about">关于</a><a style="width: 50%"  class="friends">友链</a></div>
</div>
<input type="hidden" id="yelog_site_posts_number" value="26">

<div style="display: none">
    <span id="busuanzi_value_site_uv"></span>
    <span id="busuanzi_value_site_pv"></span>
</div>

    </div>
    <div class="nav-right">
        <div class="friends-area">
    <div class="friends-title">
        友情链接
        <i class="back-title-list"></i>
    </div>
    <div class="friends-content">
        <ul>
            
            <li><a target="_blank" href="http://yelog.org/">叶落阁</a></li>
            
        </ul>
    </div>
</div>
        <div class="title-list">
    <form onkeydown="if(event.keyCode === 13){return false;}">
        <input id="local-search-input" class="search" type="text" placeholder="Search..." />
        <i class="cross"></i>
        <span>
            <label for="tagswitch">Tags:</label>
            <input id="tagswitch" type="checkbox" style="display: none" />
            <i id="tagsWitchIcon"></i>
        </span>
    </form>
    <div class="tags-list">
    
    <li class="article-tag-list-item">
        <a class="color1">linux</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">内存管理</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">伙伴系统</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">内存分配</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">内存释放</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">伙伴系统初始化</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">Slab</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">EOS</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">节点</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">数据备份</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">c语言</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color1">二分法查找</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">geth</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">私有网络搭建</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">以太坊</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">c++</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">rpc</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">code-push</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">react native</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">样式</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color4">调试宏</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color3">DPRINTF</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color2">DERROR</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">PERR</a>
    </li>
    
    <li class="article-tag-list-item">
        <a class="color5">gbk2312，拼音表，排好</a>
    </li>
    
    <div class="clearfix"></div>
</div>

    
    <nav id="title-list-nav">
        
        <a id="top" class="区块链 "
           href="/2018/09/29/EOS-%E8%8A%82%E7%82%B9%E6%95%B0%E6%8D%AE%E5%A4%87%E4%BB%BD/"
           data-tag="EOS,节点,数据备份"
           data-author="" >
            <span class="post-title" title="EOS 节点数据备份">EOS 节点数据备份</span>
            <span class="post-date" title="2018-09-29 21:51:16">2018/09/29</span>
        </a>
        
        <a id="top" class="区块链 "
           href="/2018/07/29/%E4%BB%A5%E5%A4%AA%E5%9D%8A/"
           data-tag="以太坊"
           data-author="" >
            <span class="post-title" title="以太坊">以太坊</span>
            <span class="post-date" title="2018-07-29 20:50:06">2018/07/29</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/05/18/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%901%EF%BC%9A%E6%BA%90%E4%BB%A3%E7%A0%81%E9%98%85%E8%AF%BB%E5%B7%A5%E5%85%B7%EF%BC%8C%E7%BC%96%E8%AF%91%E5%8F%8A%E8%B0%83%E8%AF%95/"
           data-tag="linux,内存管理"
           data-author="" >
            <span class="post-title" title="linux 源代码分析1:源代码阅读工具，编译及调试">linux 源代码分析1:源代码阅读工具，编译及调试</span>
            <span class="post-date" title="2013-05-18 10:18:17">2013/05/18</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/05/19/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%902%EF%BC%9A%E4%BC%99%E4%BC%B4%E7%B3%BB%E7%BB%9F/"
           data-tag="linux,内存管理,伙伴系统"
           data-author="" >
            <span class="post-title" title="linux 源代码分析2:伙伴系统">linux 源代码分析2:伙伴系统</span>
            <span class="post-date" title="2013-05-19 11:08:56">2013/05/19</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/05/20/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%903-%E4%BC%99%E4%BC%B4%E7%B3%BB%E7%BB%9F%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D/"
           data-tag="linux,内存管理,伙伴系统,内存分配"
           data-author="" >
            <span class="post-title" title="linux 源代码分析3:伙伴系统内存分配">linux 源代码分析3:伙伴系统内存分配</span>
            <span class="post-date" title="2013-05-20 19:29:04">2013/05/20</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/05/21/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%904-%E4%BC%99%E4%BC%B4%E7%B3%BB%E7%BB%9F%E5%86%85%E5%AD%98%E9%87%8A%E6%94%BE/"
           data-tag="linux,内存管理,伙伴系统,内存释放"
           data-author="" >
            <span class="post-title" title="linux 源代码分析4:伙伴系统内存释放">linux 源代码分析4:伙伴系统内存释放</span>
            <span class="post-date" title="2013-05-21 20:25:33">2013/05/21</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/05/22/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%905-%E4%BC%99%E4%BC%B4%E7%B3%BB%E7%BB%9F%E5%88%9D%E5%A7%8B%E5%8C%96/"
           data-tag="linux,内存管理,伙伴系统,伙伴系统初始化"
           data-author="" >
            <span class="post-title" title="linux 源代码分析5:伙伴系统初始化">linux 源代码分析5:伙伴系统初始化</span>
            <span class="post-date" title="2013-05-22 21:59:15">2013/05/22</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/06/03/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%906-Slab%E5%88%86%E9%85%8D%E5%99%A8/"
           data-tag="linux,内存管理,伙伴系统,Slab"
           data-author="" >
            <span class="post-title" title="linux 源代码分析6:Slab分配器">linux 源代码分析6:Slab分配器</span>
            <span class="post-date" title="2013-06-03 19:28:59">2013/06/03</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/06/05/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%907-Slab%E5%9D%97%E7%9A%84%E7%AE%A1%E7%90%86/"
           data-tag="linux,内存管理,伙伴系统,Slab"
           data-author="" >
            <span class="post-title" title="linux 源代码分析7:Slab块的管理">linux 源代码分析7:Slab块的管理</span>
            <span class="post-date" title="2013-06-05 20:56:05">2013/06/05</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/06/04/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%908-Slab%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D/"
           data-tag="linux,内存管理,伙伴系统,Slab"
           data-author="" >
            <span class="post-title" title="linux 源代码分析8:Slab内存分配">linux 源代码分析8:Slab内存分配</span>
            <span class="post-date" title="2013-06-04 19:47:29">2013/06/04</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/06/09/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%909-Slab%E5%86%85%E5%AD%98%E9%87%8A%E6%94%BE/"
           data-tag="linux,内存管理,伙伴系统,Slab"
           data-author="" >
            <span class="post-title" title="linux 源代码分析9:Slab内存释放">linux 源代码分析9:Slab内存释放</span>
            <span class="post-date" title="2013-06-09 20:17:42">2013/06/09</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/06/09/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%9010-slab%E9%80%9A%E7%94%A8%E9%95%BF%E5%BA%A6%E5%86%85%E5%AD%98%E5%88%86%E9%85%8D/"
           data-tag="linux,内存管理,伙伴系统,Slab"
           data-author="" >
            <span class="post-title" title="linux 源代码分析10:slab通用长度内存分配">linux 源代码分析10:slab通用长度内存分配</span>
            <span class="post-date" title="2013-06-09 20:17:42">2013/06/09</span>
        </a>
        
        <a id="top" class="linux "
           href="/2013/06/12/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%9011-Slab%E5%88%9D%E5%A7%8B%E5%8C%96/"
           data-tag="linux,内存管理,伙伴系统,Slab"
           data-author="" >
            <span class="post-title" title="linux 源代码分析11:Slab初始化">linux 源代码分析11:Slab初始化</span>
            <span class="post-date" title="2013-06-12 19:43:06">2013/06/12</span>
        </a>
        
        <a id="top" class="区块链 "
           href="/2020/03/29/ok6410-u-boot-dm9000-%E9%A9%B1%E5%8A%A8%E8%A1%A5%E4%B8%81-patch/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="ok6410 u-boot dm9000 驱动补丁 patch">ok6410 u-boot dm9000 驱动补丁 patch</span>
            <span class="post-date" title="2020-03-29 21:58:09">2020/03/29</span>
        </a>
        
        <a id="top" class="区块链 "
           href="/2020/03/29/eostoolkit-%E5%AE%89%E8%A3%85/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="eostoolkit 安装">eostoolkit 安装</span>
            <span class="post-date" title="2020-03-29 21:58:09">2020/03/29</span>
        </a>
        
        <a id="top" class="linux "
           href="/2020/03/29/sdl2-%E5%92%8C-dmalloc-%E4%B8%8D%E5%85%BC%E5%AE%B9/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="sdl2 和 dmalloc 不兼容">sdl2 和 dmalloc 不兼容</span>
            <span class="post-date" title="2020-03-29 21:58:09">2020/03/29</span>
        </a>
        
        <a id="top" class="区块链 "
           href="/2018/07/29/bitcoin-%E6%B5%8B%E8%AF%95%E7%BD%91%E7%BB%9C/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="bitcoin 测试网络">bitcoin 测试网络</span>
            <span class="post-date" title="2018-07-29 20:50:06">2018/07/29</span>
        </a>
        
        <a id="top" class="区块链 "
           href="/2018/07/29/USDT%E9%92%B1%E5%8C%85%E5%BD%92%E9%9B%86/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="USDT钱包归集">USDT钱包归集</span>
            <span class="post-date" title="2018-07-29 20:50:06">2018/07/29</span>
        </a>
        
        <a id="top" class="c++ "
           href="/2012/10/24/%E8%B0%83%E8%AF%95%E5%AE%8F-DPRINTF-DERROR-PERR/"
           data-tag="调试宏,DPRINTF,DERROR,PERR"
           data-author="" >
            <span class="post-title" title="调试宏 DPRINTF DERROR PERR">调试宏 DPRINTF DERROR PERR</span>
            <span class="post-date" title="2012-10-24 17:38:38">2012/10/24</span>
        </a>
        
        <a id="top" class="区块链 "
           href="/2018/08/29/%E6%AF%94%E7%89%B9%E5%B8%81%E5%AE%A2%E6%88%B7%E7%AB%AFbitcoind%E7%9A%84%E9%AB%98%E7%BA%A7%E7%94%A8%E6%B3%95/"
           data-tag=""
           data-author="" >
            <span class="post-title" title="比特币客户端bitcoind的高级用法">比特币客户端bitcoind的高级用法</span>
            <span class="post-date" title="2018-08-29 20:51:09">2018/08/29</span>
        </a>
        
        <a id="top" class="区块链 "
           href="/2016/11/23/geth%E7%A7%81%E6%9C%89%E7%BD%91%E7%BB%9C%E6%90%AD%E5%BB%BA/"
           data-tag="geth,私有网络搭建"
           data-author="" >
            <span class="post-title" title="geth私有网络搭建">geth私有网络搭建</span>
            <span class="post-date" title="2016-11-23 17:51:36">2016/11/23</span>
        </a>
        
        <a  class="react native "
           href="/2020/09/15/react-native-%E6%A0%B7%E5%BC%8F/"
           data-tag="react native,样式"
           data-author="" >
            <span class="post-title" title="react native 样式">react native 样式</span>
            <span class="post-date" title="2020-09-15 08:58:46">2020/09/15</span>
        </a>
        
        <a  class="c++ "
           href="/2020/09/15/c%E8%AF%AD%E8%A8%80-%E4%BA%8C%E5%88%86%E6%B3%95%E6%9F%A5%E6%89%BE/"
           data-tag="c语言,二分法查找"
           data-author="" >
            <span class="post-title" title="c语言 二分法查找">c语言 二分法查找</span>
            <span class="post-date" title="2020-09-15 08:51:53">2020/09/15</span>
        </a>
        
        <a  class="react native "
           href="/2020/05/19/code-push%E4%BD%BF%E7%94%A8%E8%AE%B0%E5%BD%95/"
           data-tag="code-push"
           data-author="" >
            <span class="post-title" title="code-push使用记录">code-push使用记录</span>
            <span class="post-date" title="2020-05-19 16:54:53">2020/05/19</span>
        </a>
        
        <a  class="c++ "
           href="/2018/09/15/%E4%BB%A5%E5%A4%AA%E5%9D%8Ac++%E7%89%88%E6%9C%ACrpc%E6%8E%A5%E5%8F%A3%E6%B7%BB%E5%8A%A0/"
           data-tag="以太坊,c++,rpc"
           data-author="" >
            <span class="post-title" title="以太坊c++版本rpc接口添加">以太坊c++版本rpc接口添加</span>
            <span class="post-date" title="2018-09-15 16:06:05">2018/09/15</span>
        </a>
        
        <a  class="c++ "
           href="/2011/04/28/gbk2312%E6%8B%BC%E9%9F%B3%E8%A1%A8-%E6%8C%89%E8%AF%8D%E9%A2%91%E6%8E%92%E5%A5%BD%E5%BA%8F%E7%9A%84/"
           data-tag="gbk2312，拼音表，排好"
           data-author="" >
            <span class="post-title" title="gbk2312拼音表 按词频排好序的">gbk2312拼音表 按词频排好序的</span>
            <span class="post-date" title="2011-04-28 11:21:00">2011/04/28</span>
        </a>
        
    </nav>
</div>
    </div>
    <div class="hide-list">
        <div class="semicircle">
            <div class="brackets first"><</div>
            <div class="brackets">&gt;</div>
        </div>
    </div>
</aside>
<div class="post">
    <div class="pjax">
        <article id="post-linux-3-4-10-内核内存管理源代码分析11-Slab初始化" class="article article-type-post" itemscope itemprop="blogPost">
    
        <h1 class="article-title">linux 源代码分析11:Slab初始化</h1>
    
    <div class="article-meta">
        
        <span class="top"><a>置顶</a></span>
        
        
        
        <span class="book">
            
                <a  data-rel="linux">linux</a>
            
        </span>
        
        
        <span class="tag">
            
            <a class="color1">linux</a>
            
            <a class="color5">内存管理</a>
            
            <a class="color5">伙伴系统</a>
            
            <a class="color5">Slab</a>
            
        </span>
        
    </div>
    <div class="article-meta">
        
        创建时间:<time class="date" title='更新时间: 2020-04-18 15:58:04'>2013-06-12 19:43</time>
        
    </div>
    <div class="article-meta">
        
        
        <span id="busuanzi_container_page_pv">
            阅读:<span id="busuanzi_value_page_pv">
                <span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </span>
        </span>
        
        
    </div>
    
    <div class="toc-ref">
    
        <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Slab初始化"><span class="toc-text">Slab初始化</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#kmem-cache-init函数"><span class="toc-text">kmem_cache_init函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#setup-cpu-cache函数"><span class="toc-text">setup_cpu_cache函数</span></a></li></ol></li></ol>
    
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
    .toc-level-3 i,
    .toc-level-3 ol {
        display: none !important;
    }
</style>
</div>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h1 id="Slab初始化"><a href="#Slab初始化" class="headerlink" title="Slab初始化"></a>Slab初始化</h1><pre><code>Slab的初始化由kmem_cache_init和kmem_cache_init_late两个函数完成kmem_cache_init_late在init/main.c:start_kernel中调用。kmem_cache_init的调用路径是：start_kernel-&gt;mm_init_owner-&gt;mm_init-&gt;kmem_cache_init。</code></pre><h2 id="kmem-cache-init函数"><a href="#kmem-cache-init函数" class="headerlink" title="kmem_cache_init函数"></a>kmem_cache_init函数</h2><pre><code>1497 void __init kmem_cache_init(void)
1498 {
1499        size_t left_over;
1500        struct cache_sizes *sizes;
1501        struct cache_names *names;
1502        int i;
1503        int order;
1504        int node;
1505
1506        if (num_possible_nodes() == 1)
1507                 use_alien_caches = 0;
1508
1509        for (i = 0; i &lt; NUM_INIT_LISTS; i++) {
1510                kmem_list3_init(&amp;initkmem_list3[i]);
1511                 if (i &lt; MAX_NUMNODES)
1512                        cache_cache.nodelists[i] = NULL;
1513        }
1514        set_up_list3s(&amp;cache_cache, CACHE_CACHE);
1515
1516        /*
1517         * Fragmentation resistance onlow memory - only use bigger
1518          * page orders on machines with morethan 32MB of memory if
1519          * not overridden on the command line.
1520          */
1521        if (!slab_max_order_set &amp;&amp; totalram_pages &gt; (32 &lt;&lt; 20)&gt;&gt; PAGE_SHIFT)
1522                 slab_max_order =SLAB_MAX_ORDER_HI;
1523
1524        /* Bootstrap is tricky, because several objects are allocated
1525          * from caches that do not exist yet:
1526          * 1) initialize the cache_cachecache: it contains the struct
1527          *   kmem_cache structures of all caches, except cache_cache itself:
1528          *   cache_cache is statically allocated.
1529          *   Initially an __init data area is used for the head array and the
1530          *   kmem_list3 structures, it&apos;s replaced with a kmalloc allocated
1531          *   array at the end of the bootstrap.
1532          * 2) Create the first kmalloc cache.
1533          *   The struct kmem_cache for the new cache is allocated normally.
1534          *   An __init data area is used for the head array.
1535          * 3) Create the remaining kmalloccaches, with minimally sized
1536          *   head arrays.
1537          * 4) Replace the __init data headarrays for cache_cache and the first
1538          *   kmalloc cache with kmalloc allocated arrays.
1539          * 5) Replace the __init data forkmem_list3 for cache_cache and
1540          *   the other cache&apos;s with kmalloc allocated memory.
1541          * 6) Resize the head arrays of thekmalloc caches to their final sizes.
1542          */
1543
1544        node = numa_mem_id();
1545
1546        /* 1) create the cache_cache */
1547        INIT_LIST_HEAD(&amp;cache_chain);
1548        list_add(&amp;cache_cache.next, &amp;cache_chain);
1549        cache_cache.colour_off = cache_line_size();
1550        cache_cache.array[smp_processor_id()] = &amp;initarray_cache.cache;
1551        cache_cache.nodelists[node] = &amp;initkmem_list3[CACHE_CACHE + node];
1552
1553        /*
1554          * struct kmem_cache size depends onnr_node_ids &amp; nr_cpu_ids
1555          */
1556        cache_cache.buffer_size = offsetof(struct kmem_cache, array[nr_cpu_ids])+
1557                                   nr_node_ids* sizeof(struct kmem_list3 *);
1558 #if DEBUG
1559        cache_cache.obj_size = cache_cache.buffer_size;
1560 #endif
1561        cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1562                                        cache_line_size());
1563        cache_cache.reciprocal_buffer_size =
1564                reciprocal_value(cache_cache.buffer_size);
1565
1566        for (order = 0; order &lt; MAX_ORDER; order++) {
1567                 cache_estimate(order,cache_cache.buffer_size,
1568                         cache_line_size(), 0,&amp;left_over, &amp;cache_cache.num);
1569                 if (cache_cache.num)
1570                         break;
1571        }
1572        BUG_ON(!cache_cache.num);
1573        cache_cache.gfporder = order;
1574        cache_cache.colour = left_over / cache_cache.colour_off;
1575        cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1576                                      sizeof(struct slab), cache_line_size());
1577
1578        /* 2+3) create the kmalloc caches */
1579        sizes = malloc_sizes;
1580        names = cache_names;
1581
1582        /*
1583          * Initialize the caches that providememory for the array cache and the
1584          * kmem_list3 structures first.  Without this, further allocations will
1585          * bug.
1586          */
1587
1588        sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
1589                                        sizes[INDEX_AC].cs_size,
1590                                        ARCH_KMALLOC_MINALIGN,
1591                                        ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1592                                         NULL);
1593
1594        if (INDEX_AC != INDEX_L3) {
1595                 sizes[INDEX_L3].cs_cachep =
1596                         kmem_cache_create(names[INDEX_L3].name,
1597                                sizes[INDEX_L3].cs_size,
1598                                ARCH_KMALLOC_MINALIGN,
1599                                ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1600                                 NULL);
1601        }
1602
1603        slab_early_init = 0;
1604
1605        while (sizes-&gt;cs_size != ULONG_MAX) {
1606                 /*
1607                  * For performance, all thegeneral caches are L1 aligned.
1608                 * This should beparticularly beneficial on SMP boxes, as it
1609                  * eliminates &quot;falsesharing&quot;.
1610                  * Note for systems short onmemory removing the alignment will
1611                  * allow tighter packing ofthe smaller caches.
1612                  */
1613                 if (!sizes-&gt;cs_cachep) {
1614                         sizes-&gt;cs_cachep =kmem_cache_create(names-&gt;name,
1615                                        sizes-&gt;cs_size,
1616                                         ARCH_KMALLOC_MINALIGN,
1617                                        ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1618                                         NULL);
1619                 }
1620 #ifdef CONFIG_ZONE_DMA
1621                 sizes-&gt;cs_dmacachep =kmem_cache_create(
1622                                        names-&gt;name_dma,
1623                                        sizes-&gt;cs_size,
1624                                        ARCH_KMALLOC_MINALIGN,
1625                                         ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1626                                                SLAB_PANIC,
1627                                         NULL);
1628 #endif
1629                 sizes++;
1630                 names++;
1631        }
1632        /* 4) Replace the bootstraphead arrays */
1633        {
1634                 struct array_cache *ptr;
1635
1636                 ptr = kmalloc(sizeof(structarraycache_init), GFP_NOWAIT);
1637
1638                BUG_ON(cpu_cache_get(&amp;cache_cache) != &amp;initarray_cache.cache);
1639                 memcpy(ptr,cpu_cache_get(&amp;cache_cache),
1640                        sizeof(structarraycache_init));
1641                 /*
1642                  * Do not assume thatspinlocks can be initialized via memcpy:
1643                  */
1644                spin_lock_init(&amp;ptr-&gt;lock);
1645
1646                cache_cache.array[smp_processor_id()] = ptr;
1647
1648                 ptr = kmalloc(sizeof(structarraycache_init), GFP_NOWAIT);
1649
1650                 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
1651                        !=&amp;initarray_generic.cache);
1652                 memcpy(ptr,cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
1653                        sizeof(structarraycache_init));
1654                 /*
1655                  * Do not assume thatspinlocks can be initialized via memcpy:
1656                  */
1657                spin_lock_init(&amp;ptr-&gt;lock);
1658
1659                malloc_sizes[INDEX_AC].cs_cachep-&gt;array[smp_processor_id()] =
1660                     ptr;
1661        }
1662        /* 5) Replace the bootstrap kmem_list3&apos;s */
1663        {
1664                 int nid;
1665
1666                 for_each_online_node(nid) {
1667                         init_list(&amp;cache_cache,&amp;initkmem_list3[CACHE_CACHE + nid], nid);
1668
1669                        init_list(malloc_sizes[INDEX_AC].cs_cachep,
1670                                  &amp;initkmem_list3[SIZE_AC + nid], nid);
1671
1672                         if (INDEX_AC !=INDEX_L3) {
1673                                init_list(malloc_sizes[INDEX_L3].cs_cachep,
1674                                          &amp;initkmem_list3[SIZE_L3 + nid], nid);
1675                         }
1676                 }
1677        }
1678
1679        g_cpucache_up = EARLY;
1680 }

kmem_cache_init函数的实质工作是创建一系列的slab缓存，这里包含用来分配structkmem_cache结构的缓存和通用长度缓存。关键问题是创建缓存时需要分配内存，这些内存从哪里分配呢？如果直接从伙伴系统分配，因为伙伴系统只能分配若干页，这样会造成浪费，还有一个方法就是静态分配，在系统中定义了四个有个slab的静态变量cache_cache，initkmem_list3，initarray_generic和initarray_cache。initkmem_list3定义了足以包含3个三链表数组的空间。
创建slab缓存要为struct kmem_cache结构，三链表数组和对象缓存数组分配空间。struct kmem_cache，三链表数组和对象缓存数组都是从一个slab缓存中分配空间。问题是刚开始的时候没有任何slab缓存，那这些结构的空间是从哪里来的？实际上创建第一个slab缓存的时候所有这些结构都是静态分配空间的，实际上第一个创建的是分配struct kmem_cache的slab缓存，这个slab缓存的struct kmem_cache结构用的是全局结构cache_cache，三链表使用的是initkmem_list3中的空间，对象缓存数组使用的是initarray_cache的空间。这个时候g_cpucache_up变量的值是NONE，第二个创建的是为对象缓存分配空间的slab缓存，这个时候为struct kmem_cache结构分配空间的slab缓存以及创建好，对象缓存数组使用的是initarray_generic，三链表使用的是initkmem_list3中从索引SIZE_AC开始的一段，创建第二个slab缓存的时候g_cpucache_up == NONE成立。第三个创建的是用来分配三链表数组分配空间的slab缓存，这个时候用来分配对象缓存的slab已经创建好了，但这个时候要考虑一直情况，就是如果分配三链表数组和对象缓存数组的是在同一个slab缓存，这时候三链表也可以直接从slab中分配了，如果分配三链表数组和对象缓存数组的不是在同一个slab缓存中，则第三个创建的slab缓存的三链表是以SIZE_L3为索引使用initkmem_list3的空间。
kmem_cache_init函数的代码分成5段来读
第一段：1499-1577。1514行可以看出对第零个创建的缓存的三链表初始化是以CACHE_CACHE为下标占用initkmem_list3的一段，1550行表明缓存堆栈数组是指向全局变量initarray_cache，其他对全局量cache_cache的初始化和kmem_cache_create函数大同小异。
第二段：1579-1604。第二段的任务是创建为缓存堆栈分配空间的缓存和为三链表分配内存的缓存，1594行的判断条件就是分配缓存堆栈空间和分配三链表空间的缓存是同一个的情况。
第三段：1605-1631。这一段是创建通用长度缓存。
第四段：1633-1661。这一段的工作从slab系统中分配内存替换掉刚才临时使用的全局对象缓存数组。
第五段：1663-1677。这一段的工作从slab系统中分配内存替换掉刚才临时使用的全局三链表数组。</code></pre><h2 id="setup-cpu-cache函数"><a href="#setup-cpu-cache函数" class="headerlink" title="setup_cpu_cache函数"></a>setup_cpu_cache函数</h2><pre><code>setup_cpu_cache函数是在mm/slab.c中实现代码如下：
2195 static int __init_refoksetup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
2196 {
2197        if (g_cpucache_up == FULL)
2198                 return enable_cpucache(cachep,gfp);
2199
2200        if (g_cpucache_up == NONE) {
2201                 /*
2202                  * Note: the first kmem_cache_createmust create the cache
2203                  * that&apos;s used by kmalloc(24),otherwise the creation of
2204                  * further caches will BUG().
2205                  */
2206                cachep-&gt;array[smp_processor_id()] = &amp;initarray_generic.cache;
2207
2208                 /*
2209                  * If the cache that&apos;s used bykmalloc(sizeof(kmem_list3)) is
2210                  * the first cache, then weneed to set up all its list3s,
2211                  * otherwise the creation offurther caches will BUG().
2212                  */
2213                 set_up_list3s(cachep,SIZE_AC);
2214                 if (INDEX_AC == INDEX_L3)
2215                         g_cpucache_up =PARTIAL_L3;
2216                 else
2217                         g_cpucache_up = PARTIAL_AC;
2218        } else {
2219                cachep-&gt;array[smp_processor_id()] =
2220                         kmalloc(sizeof(structarraycache_init), gfp);
2221
2222                 if (g_cpucache_up ==PARTIAL_AC) {
2223                         set_up_list3s(cachep,SIZE_L3);
2224                         g_cpucache_up =PARTIAL_L3;
2225                 } else {
2226                         int node;
2227                        for_each_online_node(node) {
2228                                cachep-&gt;nodelists[node]=
2229                                    kmalloc_node(sizeof(struct kmem_list3),
2230                                                gfp, node);
2231                                BUG_ON(!cachep-&gt;nodelists[node]);
2232                                kmem_list3_init(cachep-&gt;nodelists[node]);
2233                         }
2234                 }
2235        }
2236        cachep-&gt;nodelists[numa_mem_id()]-&gt;next_reap =
2237                         jiffies +REAPTIMEOUT_LIST3 +
2238                         ((unsignedlong)cachep) % REAPTIMEOUT_LIST3;
2239
2240        cpu_cache_get(cachep)-&gt;avail = 0;
2241        cpu_cache_get(cachep)-&gt;limit = BOOT_CPUCACHE_ENTRIES;
2242        cpu_cache_get(cachep)-&gt;batchcount = 1;
2243        cpu_cache_get(cachep)-&gt;touched = 0;
2244        cachep-&gt;batchcount = 1;
2245        cachep-&gt;limit = BOOT_CPUCACHE_ENTRIES;
2246        return 0;
2247 }

setup_cpu_cache函数是在kmem_cache_create函数中调用的，函数的功能是设置缓存的缓存堆栈和三链表结构。setup_cpu_cache函数根据变量g_cpucache_up不同的值有不同的设置方法。g_cpucache_up变量的取值是g_cpucache_up变量的值之一，在创建kmem_cache缓存的时候g_cpucache_up的值为NONE，在创建堆栈缓存的时候g_cpucache_up的值为SIZE_AC，在创建三链表缓存的时候g_cpucache_up的值为PARTIAL_L3。
2200-2218行是创建堆栈缓存时的处理代码，从这段代码可以看出，堆栈缓存的对象缓存初始化时使用的是initarray_generic的空间，三链表是以SIZE_AC为下标initkmem_list3中的空间，这从2213行代码可以看出来。2214-2217行知道如果堆栈缓存和三链表缓存是同一个缓存，这时候直接把g_cpucache_up置为PARTIAL_L3，因为这时候三链表也可以从slab中分配了。
2218-2235行，这是创建三链表堆栈的处理代码。2219-2220行，因为堆栈缓存以及创建好，这时候缓存堆栈的空间可以从slab中分配了。三链表的分配要看三链表和堆栈缓存是不是同一个，不是还有借用静态变量initkmem_list3的空间，是则可以直接在slab中分配空间了。
2236-2246是对一些变量的初始化。</code></pre>
      
       <hr><span style="font-style: italic;color: gray;"> 转载请注明来源，欢迎对文章中的引用来源进行考证，欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论，也可以邮件至 ancjf@163.com </span>
    </div>
</article>


<p>
    <a  class="dashang" onclick="dashangToggle()">赏</a>
</p>


<div class="article_copyright">
    <p><span class="copy-title">文章标题:</span>linux 源代码分析11:Slab初始化</p>
    
    <p><span class="copy-title">本文作者:</span><a  title="ancjf">ancjf</a></p>
    <p><span class="copy-title">发布时间:</span>2013-06-12, 19:43:06</p>
    <p><span class="copy-title">最后更新:</span>2020-04-18, 15:58:04</p>
    <span class="copy-title">原始链接:</span><a class="post-url" href="/2013/06/12/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%9011-Slab%E5%88%9D%E5%A7%8B%E5%8C%96/" title="linux 源代码分析11:Slab初始化">http://ancjf.com/2013/06/12/linux-3-4-10-%E5%86%85%E6%A0%B8%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86%E6%BA%90%E4%BB%A3%E7%A0%81%E5%88%86%E6%9E%9011-Slab%E5%88%9D%E5%A7%8B%E5%8C%96/</a>
    <p>
        <span class="copy-title">版权声明:</span><i class="fa fa-creative-commons"></i> <a rel="license noopener" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank" title="CC BY-NC-SA 4.0 International" target = "_blank">"署名-非商用-相同方式共享 4.0"</a> 转载请保留原文链接及作者。
    </p>
</div>





    




    </div>
    <div class="copyright">
        <p class="footer-entry">©2016-2019 Yelog</p>
<p class="footer-entry">Built with <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/yelog/hexo-theme-3-hexo" target="_blank">3-hexo</a> theme</p>

    </div>
    <div class="full-toc">
        <button class="full"><span class="min "></span></button>
<button class="post-toc-menu"><span class="post-toc-menu-icons"></span></button>
<div class="post-toc"><span class="post-toc-title">目录</span>
    <div class="post-toc-content">

    </div>
</div>
<a class="" id="rocket" ></a>

    </div>
</div>
<div class="acParent"></div>

<div class="hide_box" onclick="dashangToggle()"></div>
<div class="shang_box">
    <a class="shang_close"  onclick="dashangToggle()">×</a>
    <div class="shang_tit">
        <p>喜欢就点赞,疼爱就打赏</p>
    </div>
    <div class="shang_payimg">
        <div class="pay_img">
            <img src="/img/alipay.jpg" class="alipay" title="扫码支持">
            <img src="/img/weixin.jpg" class="weixin" title="扫码支持">
        </div>
    </div>
    <div class="shang_payselect">
        <span><label><input type="radio" name="pay" checked value="alipay">支付宝</label></span><span><label><input type="radio" name="pay" value="weixin">微信</label></span>
    </div>
</div>


</body>
<script src="/js/jquery.pjax.js?v=1.0.1" ></script>

<script src="/js/script.js?v=1.0.1" ></script>
<script>
    var img_resize = 'default';
    /*作者、标签的自动补全*/
    $(function () {
        $('.search').AutoComplete({
            'data': ['#linux','#内存管理','#伙伴系统','#内存分配','#内存释放','#伙伴系统初始化','#Slab','#EOS','#节点','#数据备份','#c语言','#二分法查找','#geth','#私有网络搭建','#以太坊','#c++','#rpc','#code-push','#react native','#样式','#调试宏','#DPRINTF','#DERROR','#PERR','#gbk2312，拼音表，排好',],
            'itemHeight': 20,
            'width': 418
        }).AutoComplete('show');
    })
    function initArticle() {
        /*渲染对应的表格样式*/
        
            $(".post .pjax table").addClass("green_title");
        

        /*渲染打赏样式*/
        
        $("input[name=pay]").on("click", function () {
            if($("input[name=pay]:checked").val()=="weixin"){
                $(".shang_box .shang_payimg .pay_img").addClass("weixin_img");
            } else {
                $(".shang_box .shang_payimg .pay_img").removeClass("weixin_img");
            }
        })
        

        /*高亮代码块行号*/
        
        $('pre code').each(function(){
            var lines = $(this).text().split('\n').length - 1, widther='';
            if (lines>99) {
                widther = 'widther'
            }
            var $numbering = $('<ul/>').addClass('pre-numbering ' + widther).attr("unselectable","on");
            $(this).addClass('has-numbering ' + widther)
                    .parent()
                    .append($numbering);
            for(var i=1;i<=lines;i++){
                $numbering.append($('<li/>').text(i));
            }
        });
        

        /*访问数量*/
        
        $.getScript("//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js");
        

        /*代码高亮，行号对齐*/
        $('.pre-numbering').css('line-height',$('.has-numbering').css('line-height'));

        
        
    }

    /*打赏页面隐藏与展示*/
    
    function dashangToggle() {
        $(".shang_box").fadeToggle();
        $(".hide_box").fadeToggle();
    }
    

</script>

<!--加入行号的高亮代码块样式-->

<style>
    pre{
        position: relative;
        margin-bottom: 24px;
        border-radius: 10px;
        border: 1px solid #e2dede;
        background: #FFF;
        overflow: hidden;
    }
    code.has-numbering{
        margin-left: 30px;
    }
    code.has-numbering.widther{
        margin-left: 35px;
    }
    .pre-numbering{
        margin: 0px;
        position: absolute;
        top: 0;
        left: 0;
        width: 20px;
        padding: 0.5em 3px 0.7em 5px;
        border-right: 1px solid #C3CCD0;
        text-align: right;
        color: #AAA;
        background-color: #fafafa;
    }
    .pre-numbering.widther {
        width: 35px;
    }
</style>

<!--自定义样式设置-->
<style>
    
    
    .nav {
        width: 542px;
    }
    .nav.fullscreen {
        margin-left: -542px;
    }
    .nav-left {
        width: 120px;
    }
    
    
    @media screen and (max-width: 1468px) {
        .nav {
            width: 492px;
        }
        .nav.fullscreen {
            margin-left: -492px;
        }
        .nav-left {
            width: 100px;
        }
    }
    
    
    @media screen and (max-width: 1024px) {
        .nav {
            width: 492px;
            margin-left: -492px
        }
        .nav.fullscreen {
            margin-left: 0;
        }
        .nav .hide-list.fullscreen {
            left: 492px
        }
    }
    
    @media screen and (max-width: 426px) {
        .nav {
            width: 100%;
        }
        .nav-left {
            width: 100%;
        }
    }
    
    
    .nav-right .title-list nav a .post-title, .nav-right .title-list #local-search-result a .post-title {
        color: #383636;
    }
    
    
    .nav-right .title-list nav a .post-date, .nav-right .title-list #local-search-result a .post-date {
        color: #5e5e5f;
    }
    
    
    .nav-right nav a.hover, #local-search-result a.hover{
        background-color: #e2e0e0;
    }
    
    

    /*列表样式*/
    
    .post .pjax article .article-entry>ol, .post .pjax article .article-entry>ul, .post .pjax article>ol, .post .pjax article>ul{
        border: #e2dede solid 1px;
        border-radius: 10px;
        padding: 10px 32px 10px 56px;
    }
    .post .pjax article .article-entry li>ol, .post .pjax article .article-entry li>ul,.post .pjax article li>ol, .post .pjax article li>ul{
        padding-top: 5px;
        padding-bottom: 5px;
    }
    .post .pjax article .article-entry>ol>li, .post .pjax article .article-entry>ul>li,.post .pjax article>ol>li, .post .pjax article>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    .post .pjax article .article-entry li>ol>li, .post .pjax article .article-entry li>ul>li,.post .pjax article li>ol>li, .post .pjax article li>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    

    /* 背景图样式 */
    
    


    /*引用块样式*/
    

    /*文章列表背景图*/
    
    .nav-right:before {
        content: ' ';
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: 0.3;
        background: url("https://i.loli.net/2019/07/22/5d3521411f3f169375.png");
        background-repeat: no-repeat;
        background-position: 50% 0;
        -ms-background-size: cover;
        -o-background-size: cover;
        -moz-background-size: cover;
        -webkit-background-size: cover;
        background-size: cover;
    }
    

    
</style>







</html>
